package com.caisejingyu.whales.common.utils;

import android.util.Base64;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 现网账号数据加密解密工具
 */
@SuppressWarnings({"WeakerAccess", "unused"})
public class DESUtil {

    private static byte[] iv = {18, 23, 53, 64, 35, 86, 37, 8};

    /**
     * 加密
     *
     * @param encryptString 明文
     * @param encryptKey    密钥
     * @return 加密后的密文
     */
    public static String encryptDES(String encryptString, String encryptKey) {
        try {
            //实例化IvParameterSpec对象，使用指定的初始化向量
            IvParameterSpec zeroIv = new IvParameterSpec(iv);
            //实例化SecretKeySpec，根据传入的密钥获得字节数组来构造SecretKeySpec
            SecretKeySpec key = new SecretKeySpec(encryptKey.getBytes(), "DES");
            //创建密码器
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            //用密钥初始化Cipher对象
            cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
            //执行加密操作
            byte[] encryptedData = cipher.doFinal(encryptString.getBytes());
            return Base64.encodeToString(encryptedData, 0);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param decryptString 密文
     * @param decryptKey    密钥
     * @return 返回明文
     */
    public static String decryptDES(String decryptString, String decryptKey) {
        try {
            //先使用Base64解密
            byte[] byteMi = Base64.decode(decryptString, 0);
            //实例化IvParameterSpec对象使用指定的初始化向量
            IvParameterSpec zeroIv = new IvParameterSpec(iv);
            //实例化SecretKeySpec，根据传入的密钥获得字节数组来构造SecretKeySpec,
            SecretKeySpec key = new SecretKeySpec(decryptKey.getBytes(), "DES");
            //创建密码器
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            //用密钥初始化Cipher对象,上面是加密，这是解密模式
            cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
            //获取解密后的数据
            byte[] decryptedData = cipher.doFinal(byteMi);
            return new String(decryptedData);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidAlgorithmParameterException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String decrypt(char[] s) {
        String secret = "pgonebatgai";
        StringBuilder sb = new StringBuilder();
        for (int i = 0, j = 0; i < s.length; i++, j++) {
            if (j >= secret.length()) {
                j = 0;
            }
            char tmp = (char) (s[i] ^ secret.charAt(j));
            sb.append(tmp);
        }
        return sb.toString();
    }

}
